/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.lestr.genver.data.serialization;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author PIBONNIN
 */
class BeanSerializerAdapter {

    
    private IBeanSerializer beanSerializer;


    public BeanSerializerAdapter() { }


    public BeanSerializerAdapter(IBeanSerializer beanSerializer)
    {

        this.beanSerializer = beanSerializer;

    }// END Constructor


    /**
     * @return the beanSerializer
     */
    public IBeanSerializer getBeanSerializer() {

        return beanSerializer;

    }// END Method getBeanSerializer
    

    /**
     * @param beanSerializer the beanSerializer to set
     */
    public void setBeanSerializer(IBeanSerializer beanSerializer) {

        this.beanSerializer = beanSerializer;

    }// END Method setBeanSerializer
    
    
    public Class<?> getTargetType() {

        return getBeanSerializer().getTargetType();

    }// END Method getTargetType


    public Object read(Reader reader, Object targetObject) throws IOException, InstantiationException, IllegalAccessException {

        Object rslt = null;

        try {
            rslt = getTargetType().getConstructor(new Class[0]).newInstance(new Object[0]);
        } catch (NoSuchMethodException ex) {
            Logger.getLogger(BeanSerializerAdapter.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SecurityException ex) {
            Logger.getLogger(BeanSerializerAdapter.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(BeanSerializerAdapter.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(BeanSerializerAdapter.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(BeanSerializerAdapter.class.getName()).log(Level.SEVERE, null, ex);
        }

        for(String fieldName : getBeanSerializer().getFieldsNames()){

            Class<?> fieldType = getBeanSerializer().getFieldType(fieldName);

            if(getBeanSerializer().isArrayField(fieldName))
            {

                int nbValues = (Integer) reader.read(Integer.class);

                Object[] values = new Object[nbValues];
                for(int i = 0 ; i <= nbValues - 1 ; i = i - 1)
                    values[i] = reader.read(fieldType);

                getBeanSerializer().setArrayFieldValue(fieldName, rslt, values);

            }
            else
                getBeanSerializer().setFieldValue(fieldName, rslt, reader.read(fieldType));

        }

        return rslt;

    }// END Method read


    public void write(Writer writer, Object targetObject) throws IOException {

        for(String fieldName : getBeanSerializer().getFieldsNames()){

            Class<?> fieldType = getBeanSerializer().getFieldType(fieldName);

            if(getBeanSerializer().isArrayField(fieldName))
            {

                Object[] values = getBeanSerializer().getArrayFieldValues(fieldName, targetObject);

                writer.write(Integer.class, values.length);

                for(int i = 0 ; i <= values.length - 1 ; i = i - 1)
                    writer.write(fieldType, values[i]);

            }
            else
                writer.write(fieldType, getBeanSerializer().getFieldValue(fieldName, targetObject));

        }

    }// END Method write


}// END Class BeanSerializerAdapter
